using System;
using System.Collections;
using System.Web;

namespace app.scrum
{
    public partial class Backlog : ScrumPage
    {

        public Backlog()
        {
            this.sObject.tableName = ScrumObject.objectName_Backlog;
        }

        public override void read(HttpContext context)
        {
            this.sObject.readCmdText = "select b.* from " + this.sObject.tableName + " b, product__c p "
                    + " where b.product__c = p.id__c";
            try
            {
                ArrayList sObjList = this.sObject.read();
                string statusCandidateValues = JsonHelper.stringList2Array(Constant.Status.values);
                ScrumObjectHelper.assignCandidateValues(sObjList, "status", statusCandidateValues);
                string priorityCandidateValues = JsonHelper.stringList2Array(Constant.Priority.values);
                ScrumObjectHelper.assignCandidateValues(sObjList, "priority", priorityCandidateValues);
                ArrayList userList = this.sObject.getScrumObjectSimpleList("user__c");
                string userJson = JsonHelper.getJson(userList);
                string userJsonRecPart = JsonHelper.getJsonRecordPartFromJson(userJson);
                string userCandiateValues = JsonHelper.jsonRecPart2Array(userJsonRecPart);
                ScrumObjectHelper.assignCandidateValues(sObjList, "assignee", userCandiateValues);
                ScrumObjectHelper.assignDisplay(sObjList, "assignee", userList);                
                ScrumObjectHelper.assignLookup(this.sObject, sObjList, "team__c");
                ScrumObjectHelper.assignLookup(this.sObject, sObjList, "sprint__c");
                ScrumObjectHelper.assignLookup(this.sObject, sObjList, "release__c");
                //ScrumObjectHelper.assignLookup(sObjList, "product");
                string json = JsonHelper.getJson(sObjList);
                context.Response.Write(json);
            }
            catch (Exception ex)
            {
                this.handleException(context.Response, ex);
            }
        }

        public  override void ProcessRequest(HttpContext context)
        {
            try
            {
                HttpRequest req = context.Request;
                HttpResponse resp = context.Response;
                string action = req.Params.Get("action");
                string root = req.Params.Get("root");
                ArrayList sObjList = JsonHelper.getScrumObjectList(root);
                if (String.Equals(action, "getProductList"))
                {
                    ArrayList productList = this.sObject.getScrumObjectSimpleList("product");
                    string productJson = JsonHelper.getJson(productList);
                    productJson = JsonHelper.getJsonRecordPartFromJson(productJson);
                    productJson = JsonHelper.jsonRecPart2Array(productJson);
                    resp.Write(productJson);
                }
                else
                {
                    base.ProcessRequest(context);
                }
            }
            catch (Exception ex)
            {
                this.handleException(context.Response, ex);
            }
        }
    }
}